<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>产品列表</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="产品列表">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<jsp:include page="../commons/top.jsp"/> 
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type='text/javascript' src='/sm/dwr/interface/managerAjax.js'></script>
 <script type='text/javascript' src='/sm/dwr/engine.js'></script>
  <script type='text/javascript'>
      function changeTypeItem(typeItemId){
          managerAjax.getSubTypeItemList(typeItemId,callBack);
      }
      
      function callBack(subTypeItemList){
          var typeItem = document.getElementById("typeItem");
          typeItem.length=0;
          typeItem.options[0] = new Option("无","0");
          if(subTypeItemList!=null && subTypeItemList.length>0){
         
              for(var i=1;i<=subTypeItemList.length;i++){
                   typeItem.options[i] = new Option(subTypeItemList[i-1].displayName,subTypeItemList[i-1].id);
              }
          }
      }
      function changeTypeItemAdd(typeItemId){
          managerAjax.getSubTypeItemList(typeItemId,callBackAdd);
      }
      
      function callBackAdd(subTypeItemList){
          var typeItem = document.getElementById("typeItemAdd");
          typeItem.length=0;
          typeItem.options[0] = new Option("无","0");
          if(subTypeItemList!=null && subTypeItemList.length>0){
              for(var i=1;i<=subTypeItemList.length;i++){
                   typeItem.options[i] = new Option(subTypeItemList[i-1].displayName,subTypeItemList[i-1].id);
              }
          }
      }
      function del(id){
          var form = document.getElementById("listForm");
          form.id.value = id;
          form.action = "/sm/controller/product/del";
          form.submit();
      }
      
      function queryTypeItem(){
          var form = document.getElementById("searchForm");
          var subTypeItemId = document.getElementById("typeItem").value;
          var parentTypeItemId = document.getElementById("parentTypeItem").value;
          if(subTypeItemId==0 && parentTypeItemId > 0){
             form.typeItemId.value = parentTypeItemId;
          }else{
             form.typeItemId.value = subTypeItemId;
          }
          
          form.submit();
      
      }
      
      function add(){
          var form = document.getElementById("addForm");
          if(isNull(form.name.value)){
              alert("请输入产品名称！");
              return false;
          }
          if(isNull(form.no.value)){
              alert("请输入产品编号！");
              return false;
          }else{
             if(!isNumberOr_Letter(form.no.value)){
                 alert("产品编号只能为数字、字母及下划线！");
                 return false;
             }             
          }
          if(!isInteger(form.currentTotalNumber.value)){
              alert("当前产品总量只能为数字！");
              return false;
          }
          if(!isInteger(form.totalNumber.value)){
               alert("产品总量只能为数字！");
              return false;
          }
          managerAjax.checkNameAndNo(form.name.value,form.no.value,addCallBack);
      }
      
      function addCallBack(result){
          var form = document.getElementById("addForm");
          var typeItem = document.getElementById("typeItemAdd").value;
          var parentTypeItem = document.getElementById("parentTypeItemAdd").value;
          if(result[0] == 'nameRepeat'){
              alert("产品名称重复，请重新添加！");
              return false;
          }else if(result[1] == 'noRepeat'){
              alert("产品编号重复，请重新添加！");
              return false;
          }else{
              if(typeItem ==0 && parentTypeItem !=null &&  parentTypeItem !=0){
                 form.typeItemId.value = parentTypeItem;
              }
              if(typeItem > 0){
                 form.typeItemId.value = typeItem ;
              }
              form.submit();
          }
      }
      function mod(id){
          window.open ('/sm/controller/product/getProduct.html?id='+id, 'newwindow', 'height=400, width=400, top=200, left=500, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no'); 
      }
  </script>

  </head>

  <body>
  <form id="searchForm" action="/sm/controller/product/list" method="get">
  <table>
  <tr><td>查询产品</td></tr></table>
  <input type="hidden" name="typeItemId" value=""/>
  
  <table>
				
				<tr>
					<td>
						产品类型：
						<select id="parentTypeItem" onchange="changeTypeItem(this.value);">
							<c:forEach var="typeItem" items="${typeItemList}">
							<c:if test="${typeItem.parentId==0}">
								<option value="${typeItem.id}">
									${typeItem.displayName}
								</option>
								</c:if>
							</c:forEach>
						</select>
					</td>
					
					<td>
						子分类：
						<select  id="typeItem">
							<option value="0">无</option>
						</select>
					</td>
					<td>
						产品名称
					</td>
					<td>
						<input type="text" name="name" value="" maxlength="100"/>
					</td>
					<td>
						<input type="button" value="查询" onclick="queryTypeItem();"/>
					</td>
				</tr>
			</table>
  </form>
  <form id="listForm" action="/sm/controller/product/del" method="POST">
  <input type="hidden" name="id" value=""/>
  <table>
  <tr><td>产品列表</td></tr></table>
  <table>
  <tr>
      <td>序号</td>
      <td>产品编号</td>
      <td>名称</td>
      <td>产品类型</td>
      <td>产品当前总量</td>
      <td>产品总量</td>
      <td>修改时间</td>
        

  </tr>
    <c:if test="${productList!=null}">
       <c:forEach var="product" items="${productList}">
          <tr>
              <td><c:if test="${sessionScope.user.role == 'admin'}"><input type="radio" name="id" value="${product.id}" onclick="mod('${product.id}');"/></c:if>${product.id}&nbsp;</td>
              <td>${product.no}&nbsp;</td>
              <td>${product.name}&nbsp;</td>
              <td>${product.typeItem.displayName }&nbsp;</td>
              <td>${product.currentTotalNumber}&nbsp;</td>
              <td>${product.totalNumber}&nbsp;</td>
              <td><fmt:formatDate value="${product.statTime}"  pattern= "yyyy-MM-dd H:mm:ss" />  &nbsp;</td>
              <!-- td><input type="button" value="删除" onclick="del(${product.id});"/></td-->
          </tr>
          
       </c:forEach>
    </c:if>
    </table>
    </form>
    <c:if test="${sessionScope.user.role == 'admin'}">
    <form id="addForm" action="/sm/controller/product/add" method="POST">
    <table>
    <input type="hidden" name="typeItemId" value="0"/>
    <tr><td>添加产品</td></tr>
        <tr>
            <td>
                                           所属分类：</td>
            <td>
                <select name="parentTypeItemId" id="parentTypeItemAdd" onchange="changeTypeItemAdd(this.value);">
               <c:forEach var="typeItem" items="${typeItemList}">
							<c:if test="${typeItem.parentId==0}">
								<option value="${typeItem.id}">
									${typeItem.displayName}
								</option>
								</c:if>
							</c:forEach>
                </select>
            </td>
            </tr>
            <tr>
            <td>
                                           子分类：</td>
            <td>
                <select id="typeItemAdd">

                </select>
            </td>
            </tr>
        <tr>
            <td>
                                         产品编号：</td>
            <td><input type="text" name="no" value="" maxlength="100"/>
            </td>
        </tr>    
        <tr>
            <td>
                                         名称：</td>
            <td><input type="text" name="name" value="" maxlength="100"/>
            </td>
            </tr>
        <tr>
            <td>
                                          产品当前总量：</td>
            <td><input type="text" name="currentTotalNumber" value="0" maxlength="10"/>
            </td>
           
        </tr>    
        <tr>
            <td>
                                          产品总量：</td>
            <td><input type="text" name="totalNumber" value="0" maxlength="10"/>
            </td>
           
        </tr>
        <tr><td>&nbsp;</td><td><input type="button" value="提交" onclick="add();"/></td></tr>
    </table>
    </form>
    </c:if>
  </body>
    <script type='text/javascript'>
        changeTypeItem(document.getElementById("parentTypeItem").value);
        <c:if test="${sessionScope.user.role == 'admin'}">
            changeTypeItemAdd(document.getElementById("parentTypeItemAdd").value);
        </c:if>
    </script> 
</html>
